package com.sskj.jd.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sskj.core.annotation.ControllerLogExeTime;
import com.sskj.core.exception.MException;
import com.sskj.dingtalk.domain.TiXingRenWuQueryParam;
import com.sskj.dingtalk.exception.OApiException;
import com.sskj.dingtalk.service.impl.DingTalkMessageServiceImpl;
import com.sskj.dingtalk.service.impl.DingTalkServiceImpl;
import com.sskj.dingtalk.utils.HttpHelper;
import com.sskj.dingtalk.vo.TiXingRenWuNeiRongVO;
import com.sskj.eng.domain.entity.DingJianDuEng;
import com.sskj.eng.domain.entity.ListEngResponse;
import com.sskj.eng.domain.entity.ListJdjlResponse;
import com.sskj.eng.domain.entity.ListResponse;
import com.sskj.eng.domain.entity.ListTiXingRenWuResponse;
import com.sskj.eng.vo.RestParamVO;
import com.sskj.eng.vo.RestQueryVO;
import com.sskj.jd.domain.JianDuJiLuQueryParam;
import com.sskj.jd.domain.entity.JianDuJiLu;
import com.sskj.jd.vo.TiXingRenWuStepVO;
import com.sskj.personnel.domain.entity.EmployeeEntity;
import com.sskj.personnel.repository.EmployeeRepository;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "/jd/txrw", method = { RequestMethod.POST, RequestMethod.GET })
@Api(value = "提醒任务管理", description = "提醒任务管理")
public class TiXingRenWuServiceImpl {

	protected final Log logger = LogFactory.getLog(DingTalkServiceImpl.class);

	@Value("${sskj.conf.restUrl}")
	private String restUrl;

	@Autowired
	EmployeeRepository employeeRepository;

	@Autowired
	DingTalkMessageServiceImpl dingTalkMessageServiceImpl;

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/count/get", method = { RequestMethod.POST })
	@ApiImplicitParams({ @ApiImplicitParam(name = "userid", value = "用户ID", paramType = "form", dataType = "string") })
	@ApiOperation(value = "获取提醒任务数量", notes = "获取提醒任务数量", httpMethod = "POST")
	public Integer getCount(@ApiIgnore TiXingRenWuQueryParam tiXingRenWuQueryParam) throws Exception {

		Integer num = 0;

		RestQueryVO data = new RestQueryVO();

		EmployeeEntity emp = employeeRepository.findByDD_Id(tiXingRenWuQueryParam.getUserid());

		if (emp != null) {
			RestParamVO param = new RestParamVO();
			param.setPn("userid");
			param.setValue(emp.getResteasy_emp_id());
			data.getParams().add(param);
		}

		RestParamVO param1 = new RestParamVO();
		param1.setPn("isRun");
		param1.setValue(false);
		data.getParams().add(param1);
		if (emp != null) {
			try {
				JSONObject obj = HttpHelper.httpPost(restUrl + "dingtalk/jd/dbrw/count", data);
				if (obj != null) {
					num = JSON.parseObject(obj.getString("count"), Integer.class);
					System.out.println(JSON.toJSONString(obj));
				}
			} catch (OApiException e) {
				e.printStackTrace();
				Object[] msg = { e.getMessage() };
				throw new MException("custom_msg", msg);
			}
		}
		else
		{
			num = 0;	
		}

		return num;

	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/get", method = { RequestMethod.POST })
	@ApiImplicitParams({ @ApiImplicitParam(name = "spId", value = "记录ID", paramType = "form", dataType = "string") })
	@ApiOperation(value = "获取提醒任务", notes = "获取提醒任务", httpMethod = "POST")
	public TiXingRenWuNeiRongVO get(@ApiIgnore TiXingRenWuQueryParam tiXingRenWuQueryParam) throws Exception {

		TiXingRenWuNeiRongVO result = null;

		RestQueryVO data = new RestQueryVO();

		RestParamVO param1 = new RestParamVO();
		param1.setPn("spId");
		param1.setValue(tiXingRenWuQueryParam.getSpId());
		data.getParams().add(param1);

		try {
			JSONObject obj = HttpHelper.httpPost(restUrl + "dingtalk/jd/dbrwmx", data);
			if (obj != null) {
				result = JSON.parseObject(obj.getString("result"), TiXingRenWuNeiRongVO.class);
				System.out.println(JSON.toJSONString(obj));
			}
		} catch (OApiException e) {
			e.printStackTrace();
			Object[] msg = { e.getMessage() };
			throw new MException("custom_msg", msg);
		}

		if (null == result) {
			throw new MException("not_data");
		} else {
			return result;
		}

	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/step/get", method = { RequestMethod.POST })
	@ApiImplicitParams({ @ApiImplicitParam(name = "glId", value = "关联ID", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "groupCode", value = "编组Code", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "currentCode", value = "当前Code", paramType = "form", dataType = "string"),

	})
	@ApiOperation(value = "获取提醒任务步骤条", notes = "获取提醒任务步骤条", httpMethod = "POST")
	public List<TiXingRenWuStepVO> getSteps(@ApiIgnore TiXingRenWuQueryParam tiXingRenWuQueryParam) throws MException {

		List<TiXingRenWuStepVO> result = new ArrayList<TiXingRenWuStepVO>();

		RestQueryVO data = new RestQueryVO();

		RestParamVO param1 = new RestParamVO();
		param1.setPn("glId");
		param1.setValue(tiXingRenWuQueryParam.getGlId());
		data.getParams().add(param1);

		RestParamVO param2 = new RestParamVO();
		param2.setPn("groupCode");
		param2.setValue(tiXingRenWuQueryParam.getGroupCode());
		data.getParams().add(param2);

		RestParamVO param3 = new RestParamVO();
		param3.setPn("currentCode");
		param3.setValue(tiXingRenWuQueryParam.getCurrentCode());
		data.getParams().add(param3);

		try {
			JSONObject obj = HttpHelper.httpPost(restUrl + "dingtalk/jd/dbrw/steps", data);
			if (obj != null) {
				result = JSON.parseArray(obj.getString("steps"), TiXingRenWuStepVO.class);
				System.out.println(JSON.toJSONString(obj));
			}
		} catch (OApiException e) {
			e.printStackTrace();
			Object[] msg = { e.getMessage() };
			throw new MException("custom_msg", msg);
		}

		if (null == result) {
			throw new MException("not_data");
		} else {
			return result;
		}

	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/send", method = { RequestMethod.POST })
	@ApiImplicitParams({ @ApiImplicitParam(name = "userid", value = "用户钉钉ID", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "currentCode", value = "当前任务CODE", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "type", value = "操作类型", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "glId", value = "关联ID", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "tjrName", value = "提交人", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "shyj", value = "审核意见", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "empName", value = "审核人", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "rwName", value = "任务名称", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "empId", value = "审核人ID", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "spId", value = "SPID", paramType = "form", dataType = "string"), })
	@ApiOperation(value = "审核操作", notes = "审核操作", httpMethod = "POST")
	public void sendShenHe(@ApiIgnore TiXingRenWuQueryParam tiXingRenWuQueryParam) throws Exception {

		TiXingRenWuNeiRongVO result = null;

		RestQueryVO data = new RestQueryVO();

		EmployeeEntity emp = employeeRepository.findByDD_Id(tiXingRenWuQueryParam.getUserid());

		if (emp != null) {
			RestParamVO param = new RestParamVO();
			param.setPn("userid");
			param.setValue(emp.getResteasy_emp_id());
			data.getParams().add(param);
		}

		RestParamVO param1 = new RestParamVO();
		param1.setPn("shjl");
		param1.setValue(tiXingRenWuQueryParam.getType());
		data.getParams().add(param1);

		RestParamVO param2 = new RestParamVO();
		param2.setPn("shyj");
		param2.setValue(tiXingRenWuQueryParam.getShyj());
		data.getParams().add(param2);

		RestParamVO param3 = new RestParamVO();
		param3.setPn("rwCode");
		param3.setValue(tiXingRenWuQueryParam.getCurrentCode());
		data.getParams().add(param3);

		RestParamVO param4 = new RestParamVO();
		param4.setPn("tjrName");
		param4.setValue(tiXingRenWuQueryParam.getTjrName());
		data.getParams().add(param4);

		RestParamVO param5 = new RestParamVO();
		param5.setPn("glId");
		param5.setValue(tiXingRenWuQueryParam.getGlId());
		data.getParams().add(param5);

		try {
			JSONObject obj = HttpHelper.httpPost(restUrl + "dingtalk/jd/dbrw/send", data);
			if (obj != null) {
				result = JSON.parseObject(obj.getString("result"), TiXingRenWuNeiRongVO.class);
				System.out.println(JSON.toJSONString(obj));
			}
			if (result != null) {
				// 发送给下一步人信息
				dingTalkMessageServiceImpl.sendOAMessage(dingTalkMessageServiceImpl.reNextShenHeMessage(result));

				// 发送给被审核人信息
				dingTalkMessageServiceImpl
						.sendOAMessage(dingTalkMessageServiceImpl.reCurrentShenHeMessage(tiXingRenWuQueryParam));

			}
		} catch (OApiException e) {
			e.printStackTrace();
			Object[] msg = { e.getMessage() };
			throw new MException("custom_msg", msg);
		}
	}

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/dsh/page", method = RequestMethod.GET)
	@ApiOperation(value = "待审核任务查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "userid", value = "用户ID", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "isRun", value = "用户ID", paramType = "form", dataType = "boolean"),
			@ApiImplicitParam(name = "pageNumber", value = "页号", paramType = "form", dataType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "页的大小", paramType = "form", dataType = "int") })
	public List<TiXingRenWuNeiRongVO> queryPage(@ApiIgnore TiXingRenWuQueryParam tiXingRenWuQueryParam,
			@ApiIgnore Pageable pageable) throws MException {

		List<TiXingRenWuNeiRongVO> result = new ArrayList<TiXingRenWuNeiRongVO>();

		RestQueryVO data = new RestQueryVO();

		data.setPage(pageable.getPageNumber() - 1);
		data.setPageSize(pageable.getPageSize());

		EmployeeEntity emp = employeeRepository.findByDD_Id(tiXingRenWuQueryParam.getUserid());

		if (emp != null) {
			RestParamVO param = new RestParamVO();
			param.setPn("userid");
			param.setValue(emp.getResteasy_emp_id());
			data.getParams().add(param);
		}

		RestParamVO param1 = new RestParamVO();
		param1.setPn("isRun");
		param1.setValue(tiXingRenWuQueryParam.getIsRun());
		data.getParams().add(param1);

		try {
			JSONObject obj = HttpHelper.httpPost(restUrl + "dingtalk/jd/dbrw", data);
			if (obj != null) {
				result = JSON.parseArray(obj.getString("resultls"), TiXingRenWuNeiRongVO.class);
				System.out.println(JSON.toJSONString(obj));
			}
		} catch (OApiException e) {
			e.printStackTrace();
			Object[] msg = { e.getMessage() };
			throw new MException("custom_msg", msg);
		}

		return result;

	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/dbsh/page")
	@ControllerLogExeTime(description = "待审核任务查询", log = false)
	@ApiOperation(value = "待审核任务", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "userid", value = "用户ID", paramType = "form", dataType = "string"),
		@ApiImplicitParam(name = "isRun", value = "用户ID", paramType = "form", dataType = "boolean"),
		@ApiImplicitParam(name = "pageNumber", value = "页号", paramType = "form", dataType = "int"),
		@ApiImplicitParam(name = "pageSize", value = "页的大小", paramType = "form", dataType = "int") })
	public ListResponse queryDbshPage(@ApiIgnore TiXingRenWuQueryParam tiXingRenWuQueryParam, Pageable pageable) {
		ListResponse rep = new ListResponse();

		List<TiXingRenWuNeiRongVO> result = new ArrayList<TiXingRenWuNeiRongVO>();

 		RestQueryVO data = new RestQueryVO();

		data.setPage(pageable.getPageNumber() - 1);
		data.setPageSize(pageable.getPageSize());

		EmployeeEntity emp = employeeRepository.findByDD_Id(tiXingRenWuQueryParam.getUserid());

		if (emp != null) {
			RestParamVO param = new RestParamVO();
			param.setPn("userid");
			param.setValue(emp.getResteasy_emp_id());
			data.getParams().add(param);
		}

		RestParamVO param1 = new RestParamVO();
		param1.setPn("isRun");
		param1.setValue(tiXingRenWuQueryParam.getIsRun());
		data.getParams().add(param1);

		try {
			JSONObject obj = HttpHelper.httpPost(restUrl + "dingtalk/jd/dbrw", data);
			if (obj != null) {
				result = JSON.parseArray(obj.getString("resultls"), TiXingRenWuNeiRongVO.class);
				rep.setSuccess(true);
				rep.setContent(new ListTiXingRenWuResponse(pageable.getPageNumber(), result));
			} 			
		} catch (OApiException e) {
			rep.setSuccess(false);			
			e.printStackTrace();
			
			Object[] msg = { e.getMessage() };
			throw new MException("custom_msg", msg);
		}

		return rep;
	}	

}
